//
//  LeftViewController.m
//  FastDoctor
//
//  Created by 王炯 on 16/7/6.
//  Copyright © 2016年 sc. All rights reserved.
//

#import "LeftViewController.h"

#import "FamousDoctorsViewController.h"
#import "CaseManagementViewController.h"
#import "FocuseDoctorViewController.h"
#import "SettingViewController.h"
#import "AboutProductViewController.h"
#import "SharedViewController.h"

#import "SignViewController.h"
#import "RegistViewController.h"
#import "PersonCenteViewController.h"
#import "Use.h"
#import <UIImageView+WebCache.h>


#define kFilePath(fileName) ([NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:fileName])

@interface LeftViewController ()<UITableViewDataSource,UITableViewDelegate>


//自定义TableView
@property (nonatomic,strong) UITableView *tableView;

//TableView的数据
@property (nonatomic,strong) NSArray *arrData1;
@property (nonatomic,strong) NSArray *arrData2;


/** 未登录界面 */
@property (nonatomic, strong) UIView *noLoginBGView;

/** 有登录界面 */
@property (nonatomic, strong) UIView *loginBGView;

@property (nonatomic,weak) UIImageView *userIcon;

@property (nonatomic,weak) UILabel *nameLabel;

@property (nonatomic,weak) UILabel *descLabel;


@property (nonatomic,strong) NSDictionary *personInfo;

@property (nonatomic,strong)UIImage *image;
@end

@implementation LeftViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
     self.personInfo = [NSKeyedUnarchiver unarchiveObjectWithFile:kFilePath(@"user.data")];
    self.image = [NSKeyedUnarchiver unarchiveObjectWithFile:kFilePath(@"image.data")];
    [self setupTableViewContent];
    
    [self setupUI];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(uploadUI) name:@"upload" object:nil];
}

#pragma mark - 设置相关界面
- (void)setupTableViewContent
{
    
    self.view.backgroundColor = WHITE_COLOR;
    self.navigationController.navigationBar.hidden = YES;
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    self.arrData1 = @[@"名医通申请",@"病例管理",@"关注医生"];
    self.arrData2 = @[@"设置与帮助",@"关于产品",@"分享给好友"];
}

//tableView的代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    if (!cell)
    {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    if (indexPath.section==0)
    {
        
        cell.textLabel.text = self.arrData1[indexPath.row];
        
    }
    else
    {
        
        cell.textLabel.text = self.arrData2[indexPath.row];
    }
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    if (section == 0)
    {
        
        return 0.01;
    }
    return 5;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSInteger section = indexPath.section;
    NSInteger row     = indexPath.row;
    
    //1.创建一个空的，下面重新赋值
    UIViewController *newVC = nil;
    
    if (section==0)
    {
        
        switch (row) {
            case 0:
                
                //2. 赋值
                newVC = (FamousDoctorsViewController *)[[FamousDoctorsViewController alloc] init];
                
                break;
                
            case 1:
                
                newVC = (CaseManagementViewController *)[[CaseManagementViewController alloc] init];
                
                break;
                
            case 2:
                
                newVC = (FocuseDoctorViewController *)[[FocuseDoctorViewController alloc] init];
                
                break;
                
            default:
                break;
        }
        
        //3.都要设置所在控制器title值
        newVC.title = self.arrData1[row];
        
    }else{
        
        switch (row) {
            case 0:
                
                newVC = (SettingViewController *)[[SettingViewController alloc] init];
                break;
                
            case 1:
                
                newVC = (AboutProductViewController *)[[AboutProductViewController alloc] init];
                break;
                
            case 2:
                
                newVC = (SharedViewController *)[[SharedViewController alloc] init];
                break;
            default:
                break;
        }
        
        
        newVC.title = self.arrData2[row];
        
    }
    
    //4. 通过centerPanel 跳转
    [self jumpToOtherViewController: newVC];
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

//懒加载tableView
- (UITableView *)tableView
{
    
    if (!_tableView)
    {
        
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        
        _tableView.dataSource = self;
        _tableView.delegate = self;
    }
    return _tableView;
}

//*******************************登录跳转的逻辑区域*********************************

//当接收到点击注销时的通知时,及时刷新UI布局
- (void)uploadUI {

    self.personInfo = [NSKeyedUnarchiver unarchiveObjectWithFile:kFilePath(@"user.data")];
    
    self.image = [NSKeyedUnarchiver unarchiveObjectWithFile:kFilePath(@"image.data")];

    [self setupUI];
    
}
  //刷新UI界面
- (void)setupUI {
    
    if ([self.personInfo[@"true_name"] length] > 0) {
        
        [self changesSignView:self.loginBGView];
        [self.noLoginBGView removeFromSuperview];
        self.nameLabel.text = self.personInfo[@"true_name"];
        self.descLabel.text = self.personInfo[@"card_number"];
        if (self.image) {
            self.userIcon.image = self.image;
        }else{
        [self.userIcon sd_setImageWithURL:[NSURL URLWithString:self.personInfo[@"head_photo"]]
                         placeholderImage:[UIImage imageNamed:@"name"]];
            [NSKeyedArchiver archiveRootObject:self.userIcon.image toFile:kFilePath(@"image.data")];
        }
    }else {
        
        [self changesSignView:self.noLoginBGView];
        [self.loginBGView removeFromSuperview];
    }
}

 //封装切换登录界面的方法
- (void)changesSignView:(UIView *)view {
    
    [self.view addSubview:view];
    
    [self.view addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make)
     {
         
         make.top.mas_equalTo(view.mas_bottom);
         make.left.bottom.mas_equalTo(0);
         make.width.mas_equalTo(SCREEN_LEFT_WIDTH);
     }];
}

 //点击用户头像时跳转到下一个界面
- (void)tapGesture:(UITapGestureRecognizer *)gesture {
    
    PersonCenteViewController *personCenter = [[PersonCenteViewController alloc]initWithStyle:UITableViewStyleGrouped];
    
    personCenter.title = self.nameLabel.text;
    
    [self jumpToOtherViewController:personCenter];
}

//*******************************登录跳转的逻辑区域*********************************

#pragma mark - 登录相关设置
-(UIView *)noLoginBGView{
    
    if (_noLoginBGView == nil) {
        _noLoginBGView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH , 120)];
        _noLoginBGView.backgroundColor = kThemeColor;
        
        //头像
        UIImageView *iconV = [[UIImageView alloc] init];
        iconV.image = [UIImage imageNamed:@"name"];
        [_noLoginBGView addSubview:iconV];
        iconV.bounds = CGRectMake(0, 0, 45, 45);
        iconV.center = CGPointMake(SCREEN_LEFT_WIDTH/2, 50);
        
        
        //登录
        UIButton *leftButton = [[UIButton alloc] init];
        [_noLoginBGView addSubview:leftButton];
        leftButton.tag = 101;
        [leftButton setTitle:@"登录" forState:UIControlStateNormal];
        [leftButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [leftButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
        
        [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(iconV.mas_bottom).offset(10);
            make.right.mas_equalTo(iconV.mas_centerX).offset(-10);
            make.size.mas_equalTo(CGSizeMake(100, 30));
        }];
        
        //注册
        UIButton *rightButton = [[UIButton alloc] init];
        [_noLoginBGView addSubview:rightButton];
        rightButton.tag = 102;
        [rightButton setTitle:@"注册" forState:UIControlStateNormal];
        [rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [rightButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
        [rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(iconV.mas_bottom).offset(10);
            make.left.mas_equalTo(iconV.mas_centerX).offset(10);
            make.size.mas_equalTo(CGSizeMake(100, 30));
        }];
        
    }
    
    return _noLoginBGView;
}

-(UIView *)loginBGView{
    
    if (_loginBGView == nil) {
        _loginBGView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH , 120)];
        _loginBGView.backgroundColor = kThemeColor;
        
        //头像
        UIImageView *iconV = [[UIImageView alloc] init];
        self.userIcon = iconV;
        
         //*******将手势添加在这里,只要是登录界面就可以点击头像跳转*****
        self.userIcon.userInteractionEnabled = YES;
        UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGesture:)];
        [self.userIcon addGestureRecognizer:gesture];
        //********************************************************
        
        [_loginBGView addSubview:iconV];
        iconV.layer.cornerRadius = 35;
        iconV.layer.masksToBounds = YES;
        
        [iconV mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.size.mas_equalTo(CGSizeMake(70, 70));
            make.left.mas_equalTo(20);
            make.top.mas_equalTo(30);
        }];
        
        //昵称
        UILabel *nameLabel = [[UILabel alloc] init];
        self.nameLabel = nameLabel;
        nameLabel.font = FONT(16);
        [_loginBGView addSubview:nameLabel];
        
        [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.equalTo(iconV).offset(5);
            make.left.mas_equalTo(iconV.mas_right).offset(15);
            
        }];
        
        //个人资料
        UILabel *descLabel = [[UILabel alloc] init];
        self.descLabel = descLabel;
        if(kDevice_Is_iPhone5){
            descLabel.font = FONT(14);
        }else{
            descLabel.font = FONT(15);
        }
        [_loginBGView addSubview:descLabel];
        [descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.bottom.equalTo(iconV).offset(-5);
            make.left.mas_equalTo(iconV.mas_right).offset(15);
            
        }];
    }
    
    return _loginBGView;
}

#pragma mark  - Actions
- (void)clickButton:(UIButton *)btn
{
    
    //点击'登录'
    if (btn.tag == 101)
    {
        
        SignViewController *loginVC = [[SignViewController alloc] init];
        
        [self jumpToOtherViewController:loginVC];
        
    }
    //点击'注册'
    else{
        
        RegistViewController *registerVC = [[RegistViewController alloc] init];
        
        [self jumpToOtherViewController:registerVC];
    }
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)dealloc {

    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

@end
